/**
 * 
 */
package com.sip.comadapter.executor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.interfaces.ReferenceDataDao;
import com.sip.dbadapter.dao.interfaces.SystemInfoDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;

/**
 * 
 *
 */
public class ReferenceDataLoadExecutor extends BaseExecutor implements Executor {

    public CommunicationMessage execute(CommunicationMessage message) {

        final CommunicationMessage communicationMessage = new CommunicationMessage();
        System.out.println("<<<<<<<<<<<<<<< ReferenceDataLoadExecutor >>>>>>>>>>>>>>>");

        // message.setMessageHeader("JOBType:cm|Executor:" + executorName + "|ID:" + serverAddress + "|Port:" + port);
        ReferenceDataDao referenceDataDao = loadReferenceDao();
        SystemInfoDao systemInfoDao = loadSystemInfoDao();

        List<Object> apiDefinitions = (List<Object>) referenceDataDao.getReferenceData("packageHierachy");
        List<Object[]> systemInfoList = (List<Object[]>) systemInfoDao.getSystemData(new String[] {"id", "systemName",});

        Map<String, Object> referenceDataMap = new HashMap<String, Object>();
        referenceDataMap.put("SYSTEM", systemInfoList);
        referenceDataMap.put("API", apiDefinitions);

        communicationMessage.setMessageHeader(message.getMessageHeader());
        communicationMessage.setMessageBody(message.getMessageBody());

        communicationMessage.setObject(referenceDataMap);

        return communicationMessage;
    }

    private ReferenceDataDao loadReferenceDao() {

        ReferenceDataDao referenceDataDao = null;
        try {
            referenceDataDao = (ReferenceDataDao) DaoLoadFactory.getDao(DaoNameConstant.REFERENCE_DATA_DAO);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return referenceDataDao;
    }

    private SystemInfoDao loadSystemInfoDao() {

        SystemInfoDao systemInfoDao = null;

        try {
            systemInfoDao = (SystemInfoDao) DaoLoadFactory.getDao(DaoNameConstant.SYSTEM_DAO);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemInfoDao;
    }

}
